package com.dongjiantong.farmrecords.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.farmrecords.enums.FarmRecordsFileEnum;
import com.dongjiantong.farmrecords.mapper.FarmRecordsFileMapper;
import com.dongjiantong.farmrecords.model.FarmRecordsFile;
import com.dongjiantong.farmrecords.service.FarmRecordsFileService;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Service(version = "1.0.0")
public class FarmRecordsFileServiceImpl extends ServiceMybatis<FarmRecordsFile> implements FarmRecordsFileService {
    @Resource
    private FarmRecordsFileMapper farmRecordsFileMapper;

    @Override
    public List<FarmRecordsFile> selectPicOrVideoListByExample(Long relevanceId, Integer type1, Integer type2) {
        try {

            if (relevanceId == null){
                throw new OPServerException(HttpCode.BAD_REQUEST,"文件关联表id不能为空,请检查");
            }
            if (type1 == null){
                throw new OPServerException(HttpCode.BAD_REQUEST,"一级类型不能为空,请检查");
            }
            if (type2 == null){
                throw new OPServerException(HttpCode.BAD_REQUEST,"二级类型不能为空,请检查");
            }

            Example example = new Example(FarmRecordsFile.class);
            example.createCriteria()
                .andEqualTo("type1", type1)
                .andEqualTo("type2",type2)
                .andEqualTo("relevance_id",relevanceId);

            List<FarmRecordsFile> farmRecordsFileList = farmRecordsFileMapper.selectByExample(example);
            return farmRecordsFileList;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 根据文件关联表id List,一级类型和二级类型查询文件列表
     * @param relevanceIds 文件关联表id
     * @param type1 一级类型
     * @param listType2 二级类型
     */
    @Override
    public List<FarmRecordsFile> selectPicOrVideoListByRelevanceIds(List<Long> relevanceIds, Integer type1, List<Integer> listType2) {
        try {

            if (relevanceIds.size() == 0){
                throw new OPServerException(HttpCode.BAD_REQUEST,"文件关联表id List不能为空,请检查");
            }
            if (type1 == null){
                throw new OPServerException(HttpCode.BAD_REQUEST,"一级类型不能为空,请检查");
            }
            if (listType2.size() == 0){
                throw new OPServerException(HttpCode.BAD_REQUEST,"二级类型不能为空,请检查");
            }

            Map<String,Object> map = Maps.newHashMap();
            map.put("relevanceIds",relevanceIds);
            map.put("type1",type1);
            if (listType2!=null){
                map.put("listType2",listType2);
            }

            List<FarmRecordsFile> farmRecordsFileList = farmRecordsFileMapper.selectPicOrVideoListByRelevanceIds(map);

            return farmRecordsFileList;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 新增单据关联的图片或者视频
     * @param type1 关联单据类型
     * @param picList 图片list
     * @param videoList 视频list
     * @param relevanceId 关联单据id
     */
    @Override
    public Map<String,Object> insertPicOrVideoByRelevanceId(Integer type1,List<String> picList, List<String> videoList, Long relevanceId) {

        if (farmRecordsFileMapper == null) {
            throw new OPServerException("farmRecordsFileMapper 服务为空,请检查");
        }
        if (relevanceId == null){
            throw new OPServerException("关联文件表id 不能为空");
        }
        if (type1 == null) {
            throw new OPServerException("关联文件类型不能为空");
        }

        Map<String,Object> result = Maps.newHashMap();

        Map<String,Object> map =Maps.newHashMap();
        map.put("type1",type1);
        map.put("relevance_id",relevanceId);

        String address = null;
        Integer type2 = null;

        if (picList != null && picList.size() > 0) {
            //插入图片
            type2 = FarmRecordsFileEnum.PHOTO.getCode();

            address = JSON.toJSONString(picList);
            map.put("type2",type2);
            map.put("address",address);

            int con = farmRecordsFileMapper.insertPicOrVideoByRelevanceId(map);

            result.put("picResult",con);
        }

        map.remove(type2);
        map.remove(address);

        if (videoList != null && videoList.size() > 0) {
            //插入视频
            type2 = FarmRecordsFileEnum.VIDEO.getCode();

            address = JSON.toJSONString(videoList);
            map.put("type2",type2);
            map.put("address",address);

            int con = farmRecordsFileMapper.insertPicOrVideoByRelevanceId(map);

            result.put("videoResult",con);
        }

        return result;
    }

    /**
     * 更新单据关联的图片或者视频
     * @param type1 关联单据类型
     * @param picList 图片list
     * @param videoList 视频list
     * @param relevanceId 关联单据id
     * @return Map<String,Object> (<picResult/图片新增结果><videoResult/视屏新增结果>)
     */
    @Override
    public Map<String, Object> updatePicOrVideoByRelevanceId(Integer type1, List picList, List videoList, Long relevanceId) {
        if (farmRecordsFileMapper == null) {
            throw new OPServerException("farmRecordsFileMapper 服务为空,请检查");
        }
        if (relevanceId == null){
            throw new OPServerException("关联文件表id 不能为空");
        }
        if (type1 == null) {
            throw new OPServerException("关联文件类型不能为空");
        }

        Map<String,Object> result = Maps.newHashMap();

        Map<String,Object> map =Maps.newHashMap();
        map.put("type1",type1);
        map.put("relevance_id",relevanceId);

        String address = null;
        Integer type2 = null;

        if (picList != null && picList.size() > 0) {
            //更新图片
            type2 = FarmRecordsFileEnum.PHOTO.getCode();

            address = JSON.toJSONString(picList);
            map.put("type2",type2);
            map.put("address",address);

            int con = farmRecordsFileMapper.updatePicOrVideoByRelevanceId(map);

            result.put("picResult",con);
        }

        map.remove(type2);
        map.remove(address);

        if (videoList != null && videoList.size() > 0) {
            //更新视频
            type2 = FarmRecordsFileEnum.VIDEO.getCode();

            address = JSON.toJSONString(videoList);
            map.put("type2",type2);
            map.put("address",address);

            int con = farmRecordsFileMapper.updatePicOrVideoByRelevanceId(map);

            result.put("videoResult",con);
        }

        return result;
    }
}